# coding=utf-8
import uuid
from django.db import models
from picture_service.models import *

# Create your models here.


class University(models.Model):
    id = models.UUIDField("编号", primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField("名称", max_length=100, null=False, blank=False)
    address = models.CharField("地址", max_length=100, null=False, blank=False)

    # TODO 大学的icon

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["name"]
        verbose_name = "大学"
        verbose_name_plural = "大学"


class Restaurant(models.Model):
    id = models.UUIDField("编号", primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField("名称", max_length=100, null=False, blank=False)
    university = models.ForeignKey("University", models.CASCADE, null=True, verbose_name="所属大学")
    # TODO 餐厅的icon或图片

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "食堂"
        verbose_name_plural = "食堂"


class Seller(models.Model):
    id = models.UUIDField("编号", primary_key=True, default=uuid.uuid4, editable=False)
    phone = models.CharField("手机号", max_length=20, null=False, blank=True, default="")
    username = models.CharField("用户名", max_length=128, unique=True, blank=False)
    password = models.CharField("密码", max_length=128, null=False, blank=False)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)
    # university = models.ForeignKey("University", models.SET_NULL)
    restaurant = models.ForeignKey("Restaurant", models.SET_NULL, null=True, verbose_name="所属食堂")
    storey = models.CharField("楼层", max_length=100, null=False, blank=True, default="")
    window = models.CharField("窗口", max_length=100, null=False, blank=True, default="")
    name = models.CharField("名称", max_length=100, null=False, blank=False)
    icon = models.ForeignKey("picture_service.PictureIdentifier", models.SET_NULL, null=True, verbose_name="图标")
    # TODO 自动获取icon编号
    # TODO 店家微信登陆信息
    grade = models.FloatField("评分", null=False, default=0)
    # TODO 顾客发布评论后更新评分
    sales = models.BigIntegerField("销售量", null=False, default=0)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "店家"
        verbose_name_plural = "店家"


class Food(models.Model):

    FOOD_STATUS = (
        (0, "VALID"),
        (1, "OFF"),
        (2, "DELETED"),
    )

    id = models.UUIDField("编号", primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField("名称", max_length=50, null=False, blank=False)
    # university = models.ForeignKey("University", models.SET_NULL)
    # restaurant = models.ForeignKey("Restaurant", models.SET_NULL)
    seller = models.ForeignKey("Seller", models.CASCADE)
    price = models.BigIntegerField("价格", null=False, blank=False)
    food_picture = models.ForeignKey("picture_service.PictureIdentifier", models.SET_NULL, null=True)
    # TODO 自动获取icon编号
    status = models.SmallIntegerField("状态", null=False, choices=FOOD_STATUS, default=0)
    grade = models.FloatField("评分", null=False, default=0)
    # TODO 顾客发布评论后更新评分
    sales = models.IntegerField("销量", null=False, default=0)
    # TODO 订单完成后更新销量
    create_time = models.DateTimeField("上架时间", auto_now_add=True)

    def __str__(self):
        return "{0}({1:.2f})".format(self.name, self.price / 100)

    class Meta:
        verbose_name = "食物"
        verbose_name_plural = "食物"
